/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NutritionInfoAdmin.java
 *
 * Created on Aug 14, 2011, 10:26:06 AM
 */
package GUI;

import Admin.InitData;
import Class.Nutrition;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Tomcat
 */
public class NutritionInfoAdmin extends javax.swing.JPanel {

    Vector data = new Vector();
    Statement st;
    ResultSet rs;
    Connection cn;
    JFrame frame;

    /** Creates new form NutritionInfoAdmin */
    public NutritionInfoAdmin(JFrame frame) {
        initComponents();
        this.frame = frame;
        data = InitData.InitNutriTableData(tblNutri);
        InitCBSearch();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void InitCBSearch() {
        cbSearch.removeAllItems();
        cbSearch.addItem((String)"ID");
        cbSearch.addItem((String)"Name");
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblNutri = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        txtKeyword = new javax.swing.JTextField();
        btnSearch = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        cbSearch = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtaNutriContent = new javax.swing.JTextArea();
        btnAddNutri = new javax.swing.JButton();
        btnUpdateNutri = new javax.swing.JButton();
        btnDeleteNutri = new javax.swing.JButton();
        btnRefresh = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(730, 406));
        setLayout(new java.awt.BorderLayout());

        jPanel1.setPreferredSize(new java.awt.Dimension(626, 25));

        jLabel1.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        jLabel1.setText("NUTRITION MANAGEMENT");
        jPanel1.add(jLabel1);

        add(jPanel1, java.awt.BorderLayout.PAGE_START);

        jPanel2.setPreferredSize(new java.awt.Dimension(270, 449));

        tblNutri.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblNutri.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblNutriMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblNutri);

        jLabel2.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        jLabel2.setText("Keyword");

        btnSearch.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        btnSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/search(2).png"))); // NOI18N
        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        jLabel3.setText("Search by");

        cbSearch.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        cbSearch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(cbSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(10, 10, 10)
                                .addComponent(btnSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE))
                            .addComponent(txtKeyword, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtKeyword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSearch)
                    .addComponent(cbSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        add(jPanel2, java.awt.BorderLayout.LINE_START);

        jPanel3.setPreferredSize(new java.awt.Dimension(420, 400));

        txtaNutriContent.setColumns(20);
        txtaNutriContent.setRows(5);
        jScrollPane2.setViewportView(txtaNutriContent);

        btnAddNutri.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        btnAddNutri.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/edit_add.png"))); // NOI18N
        btnAddNutri.setText("Add New");
        btnAddNutri.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddNutriActionPerformed(evt);
            }
        });

        btnUpdateNutri.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        btnUpdateNutri.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/add user.png"))); // NOI18N
        btnUpdateNutri.setText("Update");
        btnUpdateNutri.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateNutriActionPerformed(evt);
            }
        });

        btnDeleteNutri.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        btnDeleteNutri.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/button_cancel.png"))); // NOI18N
        btnDeleteNutri.setText("Delete");
        btnDeleteNutri.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteNutriActionPerformed(evt);
            }
        });

        btnRefresh.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        btnRefresh.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/add user.png"))); // NOI18N
        btnRefresh.setText("Refresh");
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                        .addComponent(btnAddNutri)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnUpdateNutri)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDeleteNutri)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnRefresh)))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAddNutri)
                    .addComponent(btnUpdateNutri)
                    .addComponent(btnDeleteNutri)
                    .addComponent(btnRefresh))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        add(jPanel3, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddNutriActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddNutriActionPerformed
        NutriAddNew addDialog = new NutriAddNew(frame,true);
        addDialog.setVisible(true);
        addDialog.setAlwaysOnTop(true);
        addDialog.setLocationRelativeTo(null);
}//GEN-LAST:event_btnAddNutriActionPerformed

    private void tblNutriMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblNutriMouseClicked
        int selectedRow = tblNutri.getSelectedRow();
        System.out.println(selectedRow);
        if (selectedRow > -1) {
        Vector selectedVector = (Vector) data.get(selectedRow); //Get one row which has Nutrition ID has been deleted
        String NutriID = (String) selectedVector.get(0); //Get Nutrition ID from any rows which is deleted
        rs = InitData.InitContentNutri(NutriID); //Fill Nutrition Content to TextArea
        try {
            if (rs.next()) {
                txtaNutriContent.setText(rs.getNString("NutriContent"));
            } else {
                JOptionPane.showMessageDialog(this, "Data not exists!");
            }
        } catch (SQLException ex) {
            Logger.getLogger(NutritionInfoAdmin.class.getName()).log(Level.SEVERE, null, ex);
        }
        } else {
            JOptionPane.showMessageDialog(this, "Select at least one row", "ERROR", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_tblNutriMouseClicked

    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
        data = InitData.InitNutriTableData(tblNutri);
    }//GEN-LAST:event_btnRefreshActionPerformed

    private void btnDeleteNutriActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteNutriActionPerformed
        int deleteAnswer = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete?", "Delete", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (deleteAnswer != JOptionPane.YES_OPTION) {
            return;
        }
        int resultDelete = InitData.DeleteNutri(data, tblNutri);
        if (resultDelete > 0) {
            JOptionPane.showMessageDialog(this, "Delete Nutrition Successfully!");
        } else {
            JOptionPane.showMessageDialog(this, "Delete Nutrition Fail!");
        }
    }//GEN-LAST:event_btnDeleteNutriActionPerformed

    private void btnUpdateNutriActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateNutriActionPerformed
        int[] selectedRows = tblNutri.getSelectedRows();
        if (selectedRows.length > 1) {
            JOptionPane.showMessageDialog(this, "Cannot update more than one in same time", "ERROR UPDATE", JOptionPane.ERROR_MESSAGE);
        } else if (selectedRows.length == 0) {
            JOptionPane.showMessageDialog(this, "Select at least one row", "ERROR", JOptionPane.OK_OPTION);
        } else {
            int updateNumber = selectedRows[0];
            Vector selectedVector = (Vector) data.get(updateNumber);
            String idUpdate = selectedVector.get(0).toString();
            NutritionUpdateAdmin nutriUpdateAdmin = new NutritionUpdateAdmin(idUpdate,frame,true);
            nutriUpdateAdmin.setVisible(true);
            nutriUpdateAdmin.setResizable(false);
            nutriUpdateAdmin.setLocationRelativeTo(null);
        }
    }//GEN-LAST:event_btnUpdateNutriActionPerformed

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        String keyword = txtKeyword.getText();
        int searchBy = cbSearch.getSelectedIndex();
        System.out.println(searchBy);
         Vector header = new Vector();
        Vector data = new Vector();

        Nutrition[] result = InitData.SearchNutri(keyword,searchBy);
        if (result == null) {
            JOptionPane.showMessageDialog(this, "No result found", "SEARCH RESULT", JOptionPane.OK_OPTION);
            return;
        } else {
            header.add("Nutrition ID");
            header.add("Nutrition Name");
            header.add("Image");
            header.add("Status");
            for (int i = 0; i < result.length; i++) {
                Vector row = new Vector();
                row.add(result[i].getNutriID());
                row.add(result[i].getNutriName());
                row.add(result[i].getNutriImage());
                row.add(result[i].getNutriStatus());
                data.add(row);
            }
        }
        tblNutri.setModel(new DefaultTableModel(data, header));
    }//GEN-LAST:event_btnSearchActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddNutri;
    private javax.swing.JButton btnDeleteNutri;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton btnUpdateNutri;
    private javax.swing.JComboBox cbSearch;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblNutri;
    private javax.swing.JTextField txtKeyword;
    private javax.swing.JTextArea txtaNutriContent;
    // End of variables declaration//GEN-END:variables
}
